<?php

namespace IDock\Insurance\LIHI\Response;

use IDock\Http\Response;
use IDock\Insurance\LIHI\Response\AllLIHIResponse;
use IDock\Printer\NonCar\NonCarProductListPrinter;
use IDock\Printer\NonCar\NonCarProductPrinter;
use IDock\Printer\NonCar\NonCarSubCoveragePrinter;
use IDock\Printer\UnderwritingRecallPrinter;

class NonCarProductListResponse extends AllLIHIResponse
{

    public function resolve(): Response
    {
        $body = $this->getBody();
        if (!json_decode($body)) {
            $this->setError('返回数据异常');
            return  $this;
        }
        $data = json_decode($body,true);

        if ($this->printer and  is_string($this->printer)) {
            $this->printer = new $this->printer;
        }

        $resultMess = $data['resultMessage'] ?? '';
        $status = $data['status'] ?? '';

        if(!$status) {
            $this->setError($resultMess);
            return $this;
        }
        $this->setBody($data);

        return $this;
    }

    public function printerInit(): Response
    {
        $body = $this->getBody();
        $productList = $body['planLists'] ?? [];
        $printer = new NonCarProductListPrinter();
        $list = [];
        foreach ($productList as $value){
            if(in_array($value['planName'],[
                '安行天下基础款A-2023版',
                '安行天下基础款B-2023版',
                '安行天下优选方案A-2023版',
                '安行天下优选方案A-2024版（SC)',
                '安行天下优选方案B-2023版（1-5座）',
                '安行天下优选方案B-2023版（6-7座）',
                '安行天下青铜计划-2023版（1-5座）',
                '安行天下青铜计划-2023版（6-7座）',
                '安行天下白银计划2023版（1-5座）',
                '安行天下白银计划2023版（6-7座）',
                '安行天下黄金计划2023版（1-5座）',
                '安行天下黄金计划2023版（6-7座）',
                '安行无忧-黄金计划',
                '安行无忧-白银计划',
                '安行无忧-铂金计划',
                '安行无忧-钻石计划',
                '安居无忧-如意款',
                '安康无忧 百万医疗-基础版（无社保）',
                '安康无忧 百万医疗-升级版（有社保）',
                '安康无忧 百万医疗-升级版（无社保）',
                '安康无忧 百万医疗-基础版（有社保）',
                '安居无忧-吉祥款',
                '安途全球保(家庭版)增值款',
                '安途全球保(个人版)-基本款',
                '安途全球保(家庭版)基本款',
                '安途全球保(个人版)-基本款',
                '安途全球保(个人版)-增值款',
                '摩托车驾驶人意外险-SC-计划二',
                '摩托车驾驶人意外险-SC-计划一',
            ])){
                $tmp = $value;
                unset($tmp['rations']);
                $list[] = $tmp;
                unset($tmp);
            }
        }

        foreach ($list as $item) {
            $productPrinter = new NonCarProductPrinter();
            $productPrinter->productCode = $item['proCode'] ?? '';//产品编码
            $productPrinter->productName = $item['planName'] ?? '';//产品名称
            $productPrinter->productType = '驾意险';
            $productPrinter->productDesc = '';
            $productPrinter->planCode = $item['planCode'] ?? '';//方案代码
            $productPrinter->planName = $item['planName'] ?? '';//方案名称
            $productPrinter->maxQuantity = 1;//最大购买份数
            foreach ($item['kinds'] ?? [] as $sub) {
                $subCoverage = new NonCarSubCoveragePrinter();
                $subCoverage->subCode = $sub['cvgCode'] ?? '';
                $subCoverage->subName = $sub['cvgName'] ?? '';
                $subCoverage->amount = $sub['adultMax'] ?? '';
                $productPrinter->subCoverageAppend($subCoverage);
            }
            $productPrinter->premium = $item['planPrice'];
            $productPrinter->unitAmount = $item['sumAmount'];
            $productPrinter->argc['planJson'] = json_encode($item,320);
            $printer->productListAppend($productPrinter);
        }


        $this->printer = $printer;
        return $this;
    }
}
